//
//  RuntimeViewController.m
//  MyFramework
//
//  Created by 余洋 on 2018/8/14.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "RuntimeViewController.h"
#import "SingleTableViewCell.h"
#import "PropertiesAndMethodsViewController.h"

@interface RuntimeViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSString *> *tableData;

@end

@implementation RuntimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"runtime练习";
    [self setTableData];
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI {
    [self.tableView registerNib:[UINib nibWithNibName:@"SingleTableViewCell" bundle:nil] forCellReuseIdentifier:@"SingleTableViewCell"];
    self.tableView.estimatedRowHeight = 21.f;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SingleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SingleTableViewCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"SingleTableViewCell" owner:self options:nil].lastObject;
    }
    cell.title = self.tableData[indexPath.row];
    return cell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        PropertiesAndMethodsViewController *propertiesAndMethodsVC = [[PropertiesAndMethodsViewController alloc] init];
        propertiesAndMethodsVC.type = PropertiesAndMethodTypeChangeProperties;
        propertiesAndMethodsVC.title = self.tableData[indexPath.row];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:propertiesAndMethodsVC animated:YES];
    } else if (indexPath.row == 1) {
        PropertiesAndMethodsViewController *propertiesAndMethodsVC = [[PropertiesAndMethodsViewController alloc] init];
        propertiesAndMethodsVC.type = PropertiesAndMethodTypeAddAttributesDynamically;
        propertiesAndMethodsVC.title = self.tableData[indexPath.row];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:propertiesAndMethodsVC animated:YES];
    }
}


- (void)setTableData {
    [self.tableData addObject:@"更改属性值"];
    [self.tableData addObject:@"动态添加属性"];
    [self.tableData addObject:@"动态添加方法"];
    [self.tableData addObject:@"交换方法的实现"];
    [self.tableData addObject:@"拦截并替换方法"];
    [self.tableData addObject:@"在方法上增加额外功能"];
    [self.tableData addObject:@"归档解档"];
    [self.tableData addObject:@"字典转模型"];
}

- (NSMutableArray<NSString *> *)tableData {
    if (!_tableData) {
        _tableData = [NSMutableArray array];
    }
    return _tableData;
}

@end
